package org.senseframework.support.orm.data;

import java.util.List;

/**
 * Author lanjian
 * Email  jianlan@iflytek.com
 */
public class PageData {
    private Integer pageStartIndex = 0;

    //当前页
    private Integer pageNumber = 1 - pageStartIndex;
    //下一页
    private Integer nextPageNumber;
    //前一页
    private Integer previousPageNumber;
    //最后一页
    private Integer lastPageNumber;
    //分页数量标准
    private Integer pageSize = 10;
    //总数量
    private Integer count = null;
    //前页数量
//    private Integer after_range_num = 4;
    //后页数量
//    private Integer before_range_num = 4;
    //当前页数据集合
    private List list;

//    public List<Integer> getPageNumList() {
//        List<Integer> pageNumList = new ArrayList<Integer>();
//        int to = pageNumber + before_range_num > getLastPageNumber() ? getLastPageNumber() : pageNumber + before_range_num;
//        if (pageNumber >= after_range_num) {
//            for (int i = pageNumber - after_range_num > 0 ? pageNumber - after_range_num : 1; i <= to; i++) {
//                pageNumList.add(i);
//            }
//        } else {
//            for (int i = 0; i < to; i++) {
//                pageNumList.add(i);
//            }
//        }
//        return pageNumList;
//    }


    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getBegin() {
        return (pageNumber - 1) * pageSize;
    }

    public Integer getEnd() {
        return pageNumber * pageSize >= count ? count : pageNumber * pageSize;
    }


    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getLastPageNumber() {
        lastPageNumber = (count - 1) / pageSize + 1 - pageStartIndex;
        return lastPageNumber;
    }


    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = null == pageNumber || pageNumber <= (1 - pageStartIndex) ? (1 - pageStartIndex) : pageNumber;
        this.pageNumber = this.pageNumber > getLastPageNumber() ? getLastPageNumber() : this.pageNumber;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public Integer getNextPageNumber() {
        if (pageNumber >= (1 - pageStartIndex) && pageNumber < getLastPageNumber()) {
            nextPageNumber = pageNumber + 1;
        } else {
            nextPageNumber = getLastPageNumber();
        }
        return nextPageNumber;
    }

    public Integer getPreviousPageNumber() {
        if (pageNumber > (1 - pageStartIndex) && pageNumber <= getLastPageNumber()) {
            previousPageNumber = pageNumber - 1;
        } else {
            previousPageNumber = (1 - pageStartIndex);
        }
        return previousPageNumber;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

//    public Integer getAfter_range_num() {
//        return after_range_num;
//    }
//
//    public void setAfter_range_num(Integer after_range_num) {
//        this.after_range_num = after_range_num;
//    }
//
//    public Integer getBefore_range_num() {
//        return before_range_num;
//    }
//
//    public void setBefore_range_num(Integer before_range_num) {
//        this.before_range_num = before_range_num;
//    }


    public static void main(String[] a) {
        PageData pageData = new PageData();
        pageData.setPageSize(10);
        pageData.setCount(99);
        pageData.setPageNumber(9);
        System.out.println(pageData.toString());
    }

    @Override
    public String toString() {
        return "PageData{" +
                "pageStartIndex=" + pageStartIndex +
                ", previousPageNumber=" + getPreviousPageNumber() +
                ", pageNumber=" + pageNumber +
                ", nextPageNumber=" + getNextPageNumber() +
                ", lastPageNumber=" + getLastPageNumber() +
                ", pageSize=" + pageSize +
                ", count=" + count +
                ", list=" + list +
                '}';
    }
}


